package com.ajx.todo.project.adplatform.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

import java.lang.reflect.Method;

public class PlatformAttribute {
    private static String sMacId = null;
    private static String sVerName = null;
    private static int sVerCode = -1;

    public static String getsMacId() {
        return sMacId;
    }

    public static void setsMacId(String sMacId) {
        PlatformAttribute.sMacId = sMacId;
    }

    private static String getMachineIdNative() {
        String uid = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            uid = (String) get.invoke(c, "ro.serialno");
            //uid =  android.os.SystemProperties.get("ro.serialno");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //return uid.replace('-', 'A');
        return MachineTypeHead() + uid.substring(0, 2) + uid.substring(3, uid.length());
    }

    public static String getMachineId() {
        if (sMacId != null) {
            return sMacId;
        }
        sMacId = getMachineIdNative();
        return sMacId;
    }

    public static String MachineTypeHead() {
        if (android.os.Build.MODEL.contains("K7")) {
            return "SN";
        }
        return "";
    }

    public static void reboot(Context ctx){
        if(ctx == null){
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.rd.action.reboot");
        intent.putExtra("enable", true);
        ctx.sendBroadcast(intent);
    }

    public static int getVerCode(Context ctx) {
        int verCode = sVerCode;

        if (sVerCode != -1) {
            try {
                if (ctx != null) {
                    verCode = ctx.getPackageManager().getPackageInfo(
                            ctx.getPackageName(), 0).versionCode;

                    sVerCode = verCode;
                }
            } catch (NameNotFoundException e) {
                //Log.e(TAG, "getVerCode err:"+e.getMessage());
            }
        }

        return verCode;
    }

    public static String getVerName(Context ctx) {
        String verName = sVerName;

        if (sVerName == null) {
            try {
                if (ctx != null) {
                    verName = ctx.getPackageManager().getPackageInfo(
                            ctx.getPackageName(), 0).versionName;

                    sVerName = verName;
                }
            } catch (NameNotFoundException e) {
                Log.e("PI", "getVerName err:" + e.getMessage());
            }
        }

        return verName;
    }
}
